<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" ref="refbut">
      <el-col :span="24" :xs="24">
        <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
          <el-tab-pane label="个人库" name="personal">
            <el-form :model="personalParams" ref="personalForm"  :inline="true" v-show="showPersonalSearch">
              <el-form-item label="文件名称" prop="fileName">
                <el-input v-model="personalParams.fileName" placeholder="请输入文件名称" clearable/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search"  @click="personalQuery">查询</el-button>
                <el-button icon="el-icon-refresh"  @click="personalReset">重置</el-button>
              </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8" ref="refbut">
              <el-col :span="1.5"  v-if="isFbState">
                <el-select style="margin-left:10px;" v-model="fileMJ" placeholder="请选择文件密级">
                    <el-option v-for="(item, idx) in renyuanOptions" :key="idx" :label="item.dictLabel"
                      :value="item.dictValue"></el-option>
                </el-select>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-folder-add"  @click="handleAdd" v-hasPermi="['archive:personalLibrary:add']" >添加资料</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete"  @click="handleDelete" v-hasPermi="['archive:personalLibrary:remove']" >删除资料</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-folder-add"  @click="handlePublic" v-hasPermi="['archive:personalLibrary:communalAdd']" >提升至公共资料</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-folder-add"  @click="handleSpecial" v-hasPermi="['archive:personalLibrary:zhuanTiAdd']" >添加至专题库</el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showPersonalSearch" @queryTable="getPersonalList"></right-toolbar>
            </el-row>
            <el-table v-loading="personalLoading" :data="personalList" :height="tableHeight" @selection-change="personalSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="序号" type="index" :index="table_index_personal" width="55" align="center" prop="id"/>
              <el-table-column label="文件名称" align="center" prop="fileName"/>
              <el-table-column label="密级" v-if="isFbState">
                            <template slot-scope="scope">
                              <span v-for="(i, idx) in renyuanOptions" :key="idx">
                                {{ scope.row.mj == i.dictValue ? i.dictLabel : '' }}
                            </span>
                            </template>
                        </el-table-column>
              <el-table-column label="操作时间" prop="createTime" align="center" />
              <el-table-column label="上传人员" prop="creator" align="center" />
            </el-table>
            <pagination v-show="personalTotal > 0" :total="personalTotal" :page.sync="personalParams.pageNum"
                        :limit.sync="personalParams.pageSize" @pagination="getPersonalList" />
          </el-tab-pane>


          <el-tab-pane label="公共库" name="public">
            <el-form :model="publicParams" ref="publicForm"  :inline="true" v-show="showSearch">
              <el-form-item label="文件名称" prop="fileName">
                <el-input v-model="publicParams.fileName" placeholder="请输入文件名称" clearable/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search"  @click="publicQuery">查询</el-button>
                <el-button icon="el-icon-refresh"  @click="publicReset">重置</el-button>
              </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8" ref="refbut">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-folder-add"  @click="returnPersonal" v-hasPermi="['archive:personalLibrary:communalAdd']" >退回至个人资料</el-button>
              </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getPublicList"></right-toolbar>
            </el-row>
            <el-table v-loading="publicLoading" :data="publicList" :height="tableHeight" @selection-change="publicSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="id"/>
              <el-table-column label="文件名称" align="center" prop="fileName"/>
              <el-table-column label="密级" v-if="isFbState">
                            <template slot-scope="scope">
                              <span v-for="(i, idx) in renyuanOptions" :key="idx">
                                {{ scope.row.mj == i.dictValue ? i.dictLabel : '' }}
                            </span>
                            </template>
                        </el-table-column>
              <el-table-column label="操作时间" prop="createTime" align="center" />
              <el-table-column label="上传人员" prop="creator" align="center" />
            </el-table>
            <pagination v-show="publicTotal > 0" :total="publicTotal" :page.sync="publicParams.pageNum"
                        :limit.sync="publicParams.pageSize" @pagination="getPublicList" />
          </el-tab-pane>

          <el-tab-pane label="专题库" name="special">
            <el-row :gutter="20">
              <el-col :span="4" :xs="24" ref="hMain">
                <div class="head-container" :style="{height: (treeHeight-50)+'px'}" style="overflow: auto">
                  <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
                           :filter-node-method="filterNode" :default-expanded-keys="expandedkeys"
                           :current-node-key="currentNodekey" ref="tree" node-key="typeId" default-expand-all highlight-current
                           @node-click="handleNodeClick" :style="size">
                    <template slot-scope="{ node, data }">
                      <span class="span-ellipsis" :title="node.label" :style="size">{{ node.label }}</span>
                    </template>
                  </el-tree>
                </div>
              </el-col>
              <el-col :span="20" :xs="24" >
                <div v-show="isShow">
                <el-form :model="specialParams" ref="specialForm"  :inline="true" v-show="showSpecialSearch">
                  <el-form-item label="文件名称" prop="fileName">
                    <el-input v-model="specialParams.fileName" placeholder="请输入文件名称" clearable/>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" icon="el-icon-search"  @click="specialQuery">查询</el-button>
                    <el-button icon="el-icon-refresh"  @click="specialReset">重置</el-button>
                  </el-form-item>
                </el-form>
                <el-row :gutter="10" class="mb8" ref="refbut">
                  <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-folder-add"  @click="returnPerson" v-hasPermi="['archive:personalLibrary:subject']" >退回至个人库</el-button>
                  </el-col>
                  <right-toolbar :showSearch.sync="showSpecialSearch" @queryTable="getSpecialList"></right-toolbar>
                </el-row>
                <el-table v-loading="specialLoading" :data="specialList" :height="tableHeight" @selection-change="specialSelectionChange">
                  <el-table-column type="selection" width="55" align="center" />
                  <el-table-column label="序号" type="index" :index="table_index_special" width="55" align="center" prop="id"/>
                  <el-table-column label="文件名称" align="center" prop="fileName"/>
                  <el-table-column label="密级" v-if="isFbState">
                            <template slot-scope="scope">
                              <span v-for="(i, idx) in renyuanOptions" :key="idx">
                                {{ scope.row.mj == i.dictValue ? i.dictLabel : '' }}
                            </span>
                            </template>
                        </el-table-column>
                  <el-table-column label="操作时间" prop="createTime" align="center" />
                  <el-table-column label="上传人员" prop="creator" align="center" />
                </el-table>
                <pagination v-show="specialTotal > 0" :total="specialTotal" :page.sync="specialParams.pageNum"
                            :limit.sync="specialParams.pageSize" @pagination="getSpecialList" />
                          </div>
                         
                <el-empty :image-size="200" style="margin-top:20%" v-show="!isShow"></el-empty>
            
              </el-col>
            
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-col>
    </el-row>

    <!-- 添加专题库 -->
    <el-dialog title="添加至专题库" :visible.sync="specialOpen" width="30%" append-to-body>
      <div style="overflow-y: auto; height: 250px">
        <el-tree ref="categoryTree" :data="treeData" node-key="typeId" :props="defaultProps" @node-click="specialClick" default-expand-all>
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSpecial">确 定</el-button>
        <el-button @click="specialCancel">取 消</el-button>
      </div>
    </el-dialog>

    <vimport v-if="upload.open" :upload="upload" :isType="false" @closeDelDialog="closeDelDialog1"
             @handleFileSuccess="handleFileSuccess" :uploadData="uploadData"></vimport>


  </div>
</template>

<script>
import treeHeight from "@/mixins/treeHeight";
import estimateTableHeight from "@/mixins/estimateTableHeight";
import {
  addPersonal,
  deleteInfomation,
  getdataList, getSpecialList,
  returnPerson,
  updatePublic, updateSpecial
} from "@/api/archive/bianyan/dataResources";
import {getTree} from "@/api/archive/bianyan/createTopicLibrary";
import vimport from '@/components/import/index.vue';
import {getToken} from "@/utils/auth";
import { getDicts_archive } from '@/api/archive/dict/dictManageData'
import { fbstat } from "@/api/archive/fileCollection/offlinReception.js";
import {
  getdamjbyrymj
} from "@/api/archive/fileCollection/offlinReception.js";
export default {
  name: "index",
  mixins: [treeHeight, estimateTableHeight], // 混入方法
  components: { vimport },
  data() {
    return {
      //tabs标签切换默认选择项
      activeName: 'personal',
      // 显示搜索条件
      showSearch: true,
      // 公共库查询参数
      publicParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,
        typeState: 3 // 公共
      },
      publicList: [],
      publicLoading: true,
      publicTotal: 0,
      publicIds: [],


      showSpecialSearch: true,
      deptOptions: undefined,
      //默认展开的树节点
      expandedkeys: [],
      //默认选中的树节点
      currentNodekey: "",
      defaultProps: {
        children: "children",
        label: "typeName"
      },
      typeName: undefined,
      isShow: false, //是否显示右侧数据
      specialParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,
        typeState: 2 // 专题
      },
      specialLoading: true,
      specialTotal: 0,
      specialList: [],
      specialIds: [],


      showPersonalSearch: true,
      personalParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,
        typeState: 1 // 个人
      },
      personalLoading: true,
      personalTotal: 0,
      personalList: [],
      personalIds: [],
      filename: [],

      // 添加至专题库弹窗
      specialOpen: false,
      treeData: [],
      zhuantiId: undefined,

      // 新增资料
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "新增资料",
        // 是否禁用上传
        isUploading: false,
        deptId: null,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/archive/special/subject/upload",
        // 下载模板的地址
        // urlTemplate: "/archive/minganci/importmodel"
      },
      uploadData:{},
      size: '',
      renyuanOptions: [],//密级下拉列表
      isFbState: false,
      fileMJ: undefined,
      
    }
  },
  created() {
    if (this.$store.getters.size == 'small') {
      this.size = 'font-size: 12px;'
    } else {
      this.size = 'font-size: 14px;'
    }
    this.getPersonalList()
    fbstat().then(res => {  //false=不是分保，true=是分保 
      this.isFbState = res.data
      if (this.isFbState) {
        //密级下拉列表
        getDicts_archive("MJ").then(res => { //人员密级
          getdamjbyrymj().then(resdata => { //此接口没有返回dictLabel，需要组装接口
            var mjdata = [];
            for (var i in resdata.data) {
              for (var j in res.data) {
                if (resdata.data[i].daMj == res.data[j].dictValue) {
                  mjdata.push(res.data[j])
                }
              }
            }

            this.renyuanOptions = mjdata
          })
        })
      }
    })
  },
  methods: {
    // 点击标签页
    handleClick(tab, event) {
      if(tab.name == "public") {
        this.publicParams.fileName = undefined
        this.getPublicList()
      } else if(tab.name == 'special') {
        this.getDeptTree()
      } else {
        this.personalParams.fileName = undefined
        this.getPersonalList()
      }
    },

    // 公共库查询
    publicQuery() {
      this.publicParams.pageNum = 1
      this.getPublicList()
    },
    // 公共库重置
    publicReset() {
      this.resetForm("publicForm");
      this.publicQuery()
    },
    table_index(index){
      return (this.publicParams.pageNum-1) * this.publicParams.pageSize + index + 1;
    },
    // 多选框选中数据
    publicSelectionChange(selection) {
      this.publicIds = selection.map(item => item.id)
    },
    // 查询公共库列表
    getPublicList() {
      this.publicLoading = true
      getdataList(this.publicParams).then(response=>{
        this.publicList = response.rows
        this.publicTotal = response.total
        this.publicLoading = false
      })
    },
    // 退回至个人资料
    returnPersonal(row) {
      const id = row.id || this.publicIds
      if(id.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据退回至个人资料')
      } else {
        returnPerson(id).then(response=>{
          this.$modal.msgSuccess('退回至个人资料成功')
          this.getPublicList()
        })
      }
    },


    // 专题树
    getDeptTree() {
      getTree().then(response=>{
        this.deptOptions = response.data
        if (this.deptOptions.length > 0) {
          this.currentNodeKey = this.deptOptions[0].typeId;
          this.expandedkeys.push(this.deptOptions[0].typeId);
          this.$nextTick(() => {
            this.$refs.tree.setCurrentKey(this.deptOptions[0].typeId)
          })
          if(this.currentNodeKey != '0') {
            this.isShow = true
          } else {
            this.isShow = false
          }
        }
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.typeName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if(data.parentId != '0' && data.typeId != '0' ){
        this.isShow = true
      } else {
        this.isShow = false
      }
      this.specialParams.zhuanTiID = data.typeId
      this.specialParams.fileName = undefined
      this.getSpecialList()
    },
    // 专题库查询
    specialQuery() {
      this.specialParams.pageNum = 1
      this.getSpecialList()
    },
    // 专题库重置
    specialReset() {
      this.resetForm("specialForm");
      this.specialQuery()
    },
    table_index_special(index) {
      return (this.specialParams.pageNum-1) * this.specialParams.pageSize + index + 1;
    },
    specialSelectionChange(selection) {
      this.specialIds = selection.map(item => item.id)
    },
    // 查询专题库列表
    getSpecialList() {
      this.specialLoading = true
      getSpecialList(this.specialParams).then(response=>{
        this.specialList = response.rows
        this.specialTotal = response.total
        this.specialLoading = false
      })
    },
    // 退回至个人库
    returnPerson(row) {
      const id = row.id || this.specialIds
      if(id.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据退回至个人库')
      } else {
        returnPerson(id.toString()).then(response=>{
          this.$modal.msgSuccess('退回至个人库成功')
          this.getSpecialList()
        })
      }
    },



    // 个人库查询
    personalQuery() {
      this.personalParams.pageNum = 1
      this.getPersonalList()
    },
    // 个人库重置
    personalReset() {
      this.resetForm("personalForm");
      this.personalQuery()
    },
    table_index_personal(index){
      return (this.personalParams.pageNum-1) * this.personalParams.pageSize + index + 1;
    },
    // 多选框选中数据
    personalSelectionChange(selection) {
      this.personalIds = selection.map(item => item.id)
      this.filename = selection.map(item => item.fileName)
    },
    getPersonalList() {
      this.personalLoading = true
      getdataList(this.personalParams).then(response=>{
        this.personalList = response.rows
        this.personalTotal = response.total
        this.personalLoading = false
      })
    },
    // 添加资料
    handleAdd() {
      if (this.fileMJ == undefined&&this.isFbState) {
        this.$message.warning(`请选择密级`);
        return false
      }
      
      this.upload.open = true
    },
    //导入结果返回操作
    handleFileSuccess(response) {
      this.upload.open = false;
      this.upload.isUploading = false;
      if (response.code == "500") {
        this.$message({ message: '导入失败', type: 'error' })
      } else {
        this.form = {
          fileName: response.data.name,
          filePath: response.data.url,
          typeState: '1',
          mj:this.fileMJ
        }
        addPersonal(this.form).then(response=>{
          this.getPersonalList();
          this.$modal.msgSuccess("新增成功");
        })
      }
    },
    closeDelDialog1() {
      this.upload.open = false;
    },
    // 删除资料
    handleDelete(row) {
      const id = row.id || this.personalIds
      const label = row.fileName || this.filename
      if(id.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据进行删除')
      } else {
        this.$modal.confirm('是否确认删除文件名称为"' + label + '"的数据项？').then(function() {
          return deleteInfomation(id)
        }).then(() => {
          this.getPersonalList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    // 转公共库
    handlePublic(row) {
      const id = row.id || this.personalIds
      if(id.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据提升至公共资料')
      } else {
        this.form = {
          IDs: id,
          typeState: '3'
        }
        updatePublic(this.form).then(response=>{
          this.getPersonalList();
          this.$modal.msgSuccess("提升至公共资料成功");
        })
      }
    },
    // 转专题库
    handleSpecial(row) {
      const id = row.id || this.personalIds
      if(id.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据添加至专题库')
      } else {
        getTree().then(response => {
          this.treeData = response.data
          this.specialOpen = true
        })
      }
    },
    specialClick(data) {
      if(data.typeId == '0' || data.parentId == '0') {
        this.$modal.msgWarning('请选择目录层级的节点')
      } else {
        this.zhuantiId = data.typeId
      }
    },
    submitSpecial() {
      if (this.zhuantiId == undefined) {
        this.$modal.msgWarning('请选择专题树节点')
      } else {
        const id = this.personalIds
        this.form = []
        for (let i = 0; i < id.length; i++) {
          this.form.push({
            zhuantiId: this.zhuantiId,
            gerenId: id[i],
            state: '2'
          })
        }
        updateSpecial(this.form).then(response => {
          this.getPersonalList();
          this.specialOpen = false
          this.zhuantiId = undefined
          this.$modal.msgSuccess("添加至专题库成功");
        })
      }
    },
    specialCancel() {
      this.specialOpen = false
    }
  }
}
</script>

<style scoped>
  .empty_track{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    height: calc(100vh - 222px);
    width: 100%;
  }
</style>
